﻿using System.Collections.Generic;
using System.IO;
using MedicalAssistant.core.dao;

namespace MedicalAssistant.core.dao.binary
{
    class BinPacienteDAO: BinGenericDAO<Paciente, uint>, PacienteDAO
    {
        private static BinPacienteDAO dao = null;
        private const string file = "paciente.bin";

        private BinPacienteDAO()
        {
            FileInfo file = new FileInfo(GetBinPath());
            if(!file.Exists)
            {

                List<Paciente> list = new List<Paciente>();
                MakePersistent(list);
            }
        }

        public static BinPacienteDAO GetInstance()
        {
            if(dao == null)
            {
                dao = new BinPacienteDAO();
            }
            return dao;
        }

        public override Paciente FindById(uint id)
        {
            List<Paciente> all = GetAll();
            foreach (Paciente paciente in all)
            {
                if (paciente.Prontuario.Equals(id))
                {
                    return paciente;
                }
            }
            return null;
        }

        public override Paciente Save(Paciente entity)
        {
            List<Paciente> all = GetAll();
            all.Add(entity);
            MakePersistent(all);
            return entity;
            
        }

        
        public override void Delete(Paciente entity)
        {
            if (entity == null)
            {
                return;
            }

            List<Paciente> all = GetAll();
            Paciente pacienteASerRemovido = null;
            foreach(Paciente paciente in all)
            {
                if(paciente.Prontuario.Equals(entity.Prontuario))
                {
                    pacienteASerRemovido = paciente;
                    break;
                }
            }
            all.Remove(pacienteASerRemovido);
            MakePersistent(all);
        }


        protected override string GetBinPath()
        {
            return file;
        }
    }
}